package com.yvon.service.demo.jdk.juc;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;

/**
 * @author : Yvon
 * @since : 2022-01-24
 */
public class TestCountDownLatch {
    static CountDownLatch countDownLatch;
    public static void main(String[] args) throws InterruptedException {
        countDownLatch = new CountDownLatch(3);

        for (int i = 0; i < 20; i ++ ) {
            new Thread(() -> {
                try{
                    Thread.sleep((long) (Math.random() * 3000));
                    System.out.println(Thread.currentThread().getName() + ",到餐厅了！");
                    countDownLatch.countDown();
                }catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }, "水友" + i + "号").start();
        }

        countDownLatch.await();

        System.out.println("都到齐了，那么服务员开始上菜！");
    }
}
